//
//  YSpreadView.m
//  yuimall_Iphone
//
//  Created by yuike6 yuike6 on 12-4-18.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "YSpreadView.h"
#import "header.h"

@interface YSpreadView (private) 
- (void) privateInit;
- (void) fillData2Views;
@end


@implementation YSpreadView
@synthesize items = items_;
@synthesize scrollView;

@synthesize delegate = delegate_;

-(void) dealloc
{
    YRelease(items_);
    YRelease(scrollView)
    
    YRelease(itemViews)    
    [super dealloc];
}


-(void) setItems:(NSMutableArray *)itemsValue
{
    YRelease(items_)
    items_ = [itemsValue retain];
        
    [self fillData2Views];
}

-(void) privateInit
{
    if (self.bInited) {
        return;
    }
    
    [self setBackgroundColor:RGB(230, 230, 230)];
    self->itemViews = [[NSMutableArray alloc] init];
    
    UIScrollView * tmp_scroll = [[UIScrollView alloc] init];
    
    tmp_scroll.showsVerticalScrollIndicator = NO;
    tmp_scroll.showsHorizontalScrollIndicator = NO;
    
    self.scrollView = tmp_scroll;
    YRelease(tmp_scroll);
    [self addSubview:self.scrollView];
    
    [self reAdjustLayout];
    self.bInited = TRUE;
}

-(id) init
{
    self = [super init];
    if (self) 
    {
        [self privateInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}


-(void) reAdjustLayout
{
    [self.scrollView setFrame:self.bounds];
}


#pragma mark -
#pragma mark - Private Method

-(CGFloat) itemViewHeight;
{
    CGFloat const height = _SUBJECT_ITEM_HEIGHT;
    return height;
}

-(CGRect) itemFrame:(NSInteger) itemIndex;
{
    
    CGFloat const height = [self itemViewHeight];
    
    CGRect frame = CGRectZero;    
    frame.origin.x = 0;
    frame.origin.y = itemIndex * height;
    
    frame.size.width = self.width;
    frame.size.height = height;
    return frame;
}

-(void) setConentScrollSize:(CGSize)size
{
    
    float height = 0;
    for (UIView * view in self->itemViews) {
        height += view.frame.size.height;
    }
    
    if (height <= self.height) {
        height  = self.height + 1;
    }
   
    CGSize size_ = CGSizeZero;
    size_.height = height;
    size_.width = size.width;
    [self.scrollView setContentSize:size_];
}

-(void) fillData2Views
{
    [self->itemViews removeAllObjects];
    
    for (YSpreadViewModel * itemData in self.items) 
    {
        YSpreadViewItemView * itemView = [[YSpreadViewItemView alloc] init];  
                
        [itemView setTag:[self->itemViews count]];
        [itemView setDelegate:self];
        [itemView setData:itemData];        
        [itemView setFrame:[self itemFrame:[self->itemViews count]]];
        
        [self.scrollView addSubview:itemView];
        [self->itemViews addObject:itemView];
        
        YRelease(itemView);
        
        [self setConentScrollSize: CGSizeMake(self.width, itemView.origin_y + itemView.height)];
    }
    
}

#pragma mark -
#pragma mark - YSpreadViewItemViewDelegate
- (void) spread:(YSpreadViewItemView *) itemView
{
    int whichViewSpread = itemView.tag;
    
    if (whichViewSpread < 0 ||
        whichViewSpread >= [self->itemViews count]) 
    {
        return;
    }
    
    [[YkAnimationStep for:0.3f animate:^{

        for (int i = whichViewSpread + 1; i < [self->itemViews count]; i++) 
        {
            YSpreadViewItemView * view = [self->itemViews objectAtIndex:i];
            
            CGRect frame = view.frame;        
            frame.origin.y += itemView.height - [self itemViewHeight];        
            
            [view setFrame:frame];
        }   
        
        [self setConentScrollSize:self.frame.size];

    }] runAnimation];
}

- (void) hideSubItemPan:(YSpreadViewItemView *) itemView
{
    YHIDEVIEW(itemView.subItemPanl)
}

- (void) restore:(YSpreadViewItemView *) itemView
{
    int whichViewSpread = itemView.tag;
    
    if (whichViewSpread < 0 ||
        whichViewSpread >= [self->itemViews count]) 
    {
        return;
    }
        
    
    [[YkAnimationStep for:0.3 animate:^{
        for (int i = whichViewSpread + 1 ; i < [self->itemViews count]; i++) 
        {
            YSpreadViewItemView * view = [self->itemViews objectAtIndex:i];
                        
            CGRect frame = view.frame;
            
            frame.origin.y = [self itemViewHeight] * (i - whichViewSpread) + itemView.origin_y;  
            [view setFrame:frame];
        }
        
        [self setConentScrollSize:self.frame.size];

    }] runAnimation];
    
    [self performSelector:@selector(hideSubItemPan:) withObject:itemView afterDelay:0.5f];
}

- (void) clickWhichSubItem: (YSpreadViewItemViewSubjectView *) subjectView subItemView:(YSpreadViewItemViewSubViewItemView *) whichView
{
    if (self.delegate && 
        [self.delegate respondsToSelector:@selector(clickWhichSubItem:subItemView:)]) {
        [self.delegate clickWhichSubItem:subjectView subItemView:whichView];
    }
}

- (void) subjectImageClick:(YSpreadViewItemViewSubjectView *) subjectView
{
    DELEGATE_CALLBACK(subjectImageClick:, subjectView);
}

-(BOOL) isNeedShowHighLight:(YSpreadViewItemViewSubjectView *) subjectView;
{
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(isNeedShowHighLight:)]) {
        return [self.delegate isNeedShowHighLight:subjectView];
    }
    return FALSE;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
